﻿Imports System.Windows.Forms
Imports System.Data.OleDb

Public Class DeleteQueryDialog

    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
        DeleteQuery(CInt(txtDel.Text)) 'Call DeleteQuery Function with user given QID as an arg
        Me.Close()
    End Sub

    Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

    Private Sub DeleteQuery(ByVal qidToDelete As Integer)
        Dim makeSure As MsgBoxResult = MsgBox("Are you sure?", MsgBoxStyle.YesNo) 'Last Chance for user to cancel deleting
        Select Case makeSure 'Salect cases....

            Case MsgBoxResult.Yes 'case user selects yes....

                Dim selectCommand As String = "SELECT * FROM RecFinder_Log" 'query to get all records
                Dim conn As New OleDbConnection 'the connector

                If My.Settings.RegistryAccessible = True Then 'if registry is accessble
                    conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & My.Computer.Registry.GetValue("HKEY_LOCAL_MACHINE\Software\RecFinder\recfinder_connString", "path", Nothing) 'use registry path
                Else
                    conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Main.defaultPath 'else use default path
                End If



                Using adapter As New OleDbDataAdapter(selectCommand, conn) 'use adapter to fill a data table


                    Dim dt As New DataTable ' a new datatable
                    Dim comb As New OleDbCommandBuilder(adapter) 'Command Builder, this will generate the DELETE command
                    adapter.Fill(dt) 'fill the datatable with the DB contents
                    Dim matchFound As Boolean = False 'this will be used as a flag when searching for match to delete

                    Try 'Try...
                        For Each row As DataRow In dt.Rows() 'For each row in datatable
                            If row.Item("QID") = qidToDelete Then 'if qid field equals user given qid
                                matchFound = True 'mark flag as true
                                row.Delete() 'delete the row from datatable
                                adapter.Update(dt) 'update the Database
                                MsgBox("Deleted Successfully", MsgBoxStyle.Information) 'notify user
                                qDbManager.Close() 'close form
                                qDbManager.Show() 're-open it, to refresh it's contents
                                Exit Sub 'exit, no need to look further, 2 same quids will not exist anyway
                            Else
                                matchFound = False 'if no match found, mark flag as false
                            End If
                        Next


                        If matchFound = False Then 'if no match found...
                            MsgBox("No match found", MsgBoxStyle.Exclamation) 'Notify user
                        End If


                    Catch ex As Exception 'In case something goes wrong
                        MsgBox(String.Format("An Error Occured during deleting. Reason is {0}", ex.Message), MsgBoxStyle.Critical) 'Notify user....
                    End Try


                End Using

            Case MsgBoxResult.No 'If user selects no...
                Return 'Stop right there, and get out of here!

        End Select

    End Sub

End Class
